<?php
/**
 * User:姜亮
 * Date: 2018/8/31 ;Time: 8:46;
 * 权限校验
 */

namespace app\admin\controller;

use think\Controller;

use app\admin\model\AuthRule;

class Permiss extends Controller
{
    //查看
    public function show()
    {
        $authRules = AuthRule::all(function ($query) {
            $query->order('id', 'asc');
        });
        $this->assign([
            'authRules' => $authRules,
            'count' => count($authRules)
        ]);
        return $this->fetch('permiss-list');
    }

    //添加
    public function permiss_add()
    {
        //标识
        $tag = input('permiss_tag');
        if (empty($tag)) {
            return $this->fetch();
        } else {
            $form = input('post.');
            $authRule=new AuthRule();
            $authRule->name=$form['permissField'];
            $authRule->title=$form['permissName'];
            $res=$authRule->save();
            if ($res !== false) {
                return xmall_msg(200, 'ok');
            } else {
                return xmall_msg(201, 'fail');
            }
        }

    }

    //修改
    public function permiss_update()
    {
        //标识
        $tag = input('permiss_tag');
        if (empty($tag)) {
            //查询当前id
            $authGroup = AuthRule::get(input('id'));
            $this->assign([
                'authGroup' => $authGroup,
            ]);
            return $this->fetch('permiss_update');
        }
        else {
            //添加更改
            $form = input('post.');
            $authRule = AuthRule::get($form['id']);
            $authRule->title = $form['permissName'];
            $authRule->name = $form['permissField'];
            $res = $authRule->save();
            if ($res !== false) {
                return xmall_msg(200, 'ok');
            } else {
                return xmall_msg(201, 'fail');
            }
        }
    }

    //删除
    public function permiss_delete()
    {
        $ids = input('ids');
        if (empty($ids)) {
            $id= input('id');
            $res=AuthRule::get($id)->delete();
            if ($res !== false) {
                return xmall_msg(200, 'ok');
            } else {
                return xmall_msg(201, 'fail');
            }
        }else{
            $res=AuthRule::destroy($ids);
            if ($res>0){
                return xmall_msg(200, 'ok');
            }else{
                return xmall_msg(201, 'fail');
            }
        }
    }
}